import unittest
from random import randint
from faker import Faker
from time import sleep
from login.oa import login
from random_cn import cn
fa = Faker(locale='zh_CN')
class Oa_gxx(unittest.TestCase):  #公共信息
    def setUp(self):
        self.driver = login()
    def tearDown(self):
        sleep(2)
        self.driver.close()
    def test_01(self):
        #进入公共信息-发布公告
        self.driver.find_element('xpath', '//*[@id="leftmenuDiv"]/h3[2]/span').click()
        self.driver.find_element('xpath', '//*[@id="leftmenuDiv"]/div[2]/table/tbody/tr[3]/td/a').click()
        self.driver.switch_to.frame(self.driver.find_element('xpath', '//*[@id="iframe_main"]'))
        self.driver.find_element('xpath', '//*[@id="subject"]').send_keys(f'{cn()}')
        # 发布范围
        a = randint(1,2)
        sleep(2)
        if a == 1:
            self.driver.find_element('id', 'trackInput').click()        #发布范围
            self.driver.find_element('xpath', f'/html/body/div[3]/div[{a}]/button').click()#全部员工
        else:
            self.driver.find_element('id', 'trackInput').click()
            self.driver.find_element('xpath', f'/html/body/div[3]/div[{a}]/button').click()  # 部分员工
            self.driver.switch_to.default_content()
            self.driver.switch_to.frame(self.driver.find_element('xpath', '/html/body/div[1]/div/table/tbody/tr[2]/td[2]/div/table/tbody/tr[2]/td/div/iframe'))
            self.driver.find_element('id','DeptTreeDiv_3_span').click()
            self.driver.find_element('xpath','/html/body/div[5]/button[2]').click()
            self.driver.find_element('xpath','/html/body/div[8]/button[1]').click()
        #公告类型
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(self.driver.find_element('name','iframe_main'))
        b = randint(262,263)
        if b == 262:
            self.driver.find_element('xpath', '//*[@id="categoryId"]/option[1]').click()
        else:
            self.driver.find_element('xpath', '//*[@id="categoryId"]/option[2]').click()
        #进入编辑
        self.driver.switch_to.frame(self.driver.find_element('id', 'baidu_editor_0'))
        self.driver.find_element('class name', 'view').send_keys(f'{cn()}')
        #添加附件
        c = randint(1,2)
        if c == 1:
            self.driver.switch_to.parent_frame()
            self.driver.find_element('id', 'button_attach').click()
            self.driver.switch_to.default_content()
            self.driver.switch_to.frame(self.driver.find_element('xpath',
                                                             '/html/body/div[1]/div/table/tbody/tr[2]/td[2]/div/table/tbody/tr[2]/td/div/iframe'))
            self.driver.find_element('id', 'file').send_keys(r"F:\pycharm\oa-system\picture\123.png")
            sleep(2)
            self.driver.find_element('xpath', '//*[@id="button_upload"]').click()
            sleep(5)
            self.driver.switch_to.default_content()
            self.driver.find_element('class name', 'd-close').click()
            sleep(2)
        else:
            pass
        #发布公告
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(self.driver.find_element('xpath', '//*[@id="iframe_main"]'))
        self.driver.find_element('id', 'button_send').click()  #发布
        self.driver.switch_to.default_content()
        #断言
        assert self.driver.find_element('xpath',
                                        '/html/body/div[1]/div/table/tbody/tr[2]/td[2]/div/table/tbody/tr[2]/td/div/table/tbody/tr/td[2]').text == '发布成功！', '发布失败'
        self.driver.find_element('class name', 'd-button').click()  #确认


if __name__ == '__main__':
    unittest.main()

